<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script src="../assets/js/polygon.min.js"></script> 
<script type="text/javascript">
	var peta;
	var gambar_tanda;
	gambar_tanda = '../assets/img/pointer.png';
	function peta_awal(){
	    var lokasibaru = new google.maps.LatLng(-6.912889,107.609787);
    	var petaoption = {
			zoom: 12,
			center: lokasibaru,
			mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        
	    peta = new google.maps.Map(document.getElementById("map_canvas"),petaoption);
	    
	    tanda = new google.maps.Marker({
	        position: lokasibaru,
	        map: peta, 
	        icon: gambar_tanda,
	        draggable : true
	    });
	    
	     var creator = new PolygonCreator(peta);
		 
		 //reset
		 $('#reset').click(function(){ 
		 		creator.destroy();
		 		creator=null;
		 		creator=new PolygonCreator(peta);
		 });		 
		 
		 
		 //show paths
		 $('#showData').click(function(){ 
		 		$('#dataPanel').empty();
		 		if(null==creator.showData()){
		 			$('#dataPanel').append('Please first create a polygon');
		 		}else{
		 			$('#dataPanel').append(creator.showData());
		 		}
		 });
	    
		 
	    google.maps.event.addListener(tanda, 'dragend', function(event){
				document.getElementById('latitude').value = this.getPosition().lat();
				document.getElementById('longitude').value = this.getPosition().lng();
		});
	}
</script>
<?php

$o = "";

   if (isset($_GET['success']) && ($_GET['success'] == "1")) {

		$o .= '<div class="alert alert-success">
				Proses tambah kabupaten berhasil
				</div>';
				
	} elseif (isset($_GET['success']) && ($_GET['success'] == "0")) {
		
		$o .= '<div class="alert alert-error">
				Proses tambah kabupaten gagal
			   </div>';
	}
	
echo $o;

?>
<div class="row">
	<div class="span8">
		<div class="control-group">
			<div id="map_canvas" style="width:100%; height:500px"></div>
		</div>
		<a href="?page=kabupaten"><i class=" icon-arrow-left"></i> kembali</a>
	</div>
	<div class="span4">
	<form class="form-horizontal" id="registerHere" method='post' action='?page=addkab&action=add'>
	 <fieldset>
		<legend>TAMBAH KABUPATEN</legend>
		<div class="control-group">
		  <label class="control-label" for="input01">Kabupaten</label>
		  <div class="controls">
			<input type="text" class="input-xlarge" id="kabupaten" name="kabupaten" rel="popover" data-content="Masukkan nama kabupaten." data-original-title="Kabupaten">
		  </div>
		</div>

		<div class="control-group">
		<label class="control-label" for="input01">Luas Wilayah</label>
		  <div class="controls">
			<input type="text" class="input-xlarge" id="luas_wilayah" name="luas_wilayah" rel="popover" data-content="Masukkan luas wilayah" data-original-title="Luas Wilayah">
		   
		  </div>
		</div>
		
		<div class="control-group">
		<label class="control-label" for="input01">Kepadatan Penduduk</label>
		  <div class="controls">
			<input type="text" class="input-xlarge" id="kepadatan_penduduk" name="kepadatan_penduduk" rel="popover" data-content="Masukkan jumlah kepadatan penduduk" data-original-title="Kepadatan Penduduk">
		   
		  </div>
		</div>
		
		<div class="control-group">
		<label class="control-label" for="input01">Tata Guna Lahan</label>
		  <div class="controls">
			<input type="text" class="input-xlarge" id="tata_guna_lahan" name="tata_guna_lahan" rel="popover" data-content="Masukkan tata guna lahan" data-original-title="Tata Guna Lahan">
		   
		  </div>
		</div>
		
		<div class="control-group">
		<label class="control-label" for="input01">Topografi Wilayah</label>
		  <div class="controls">
			<input type="text" class="input-xlarge" id="tofografi_wilayah" name="tofografi_wilayah" rel="popover" data-content="Masukkan jumlah topografi wilayah" data-original-title="Topografi Wilayah">
		   
		  </div>
		</div>
		
		<div class="control-group">
		<label class="control-label" for="input01">Sumber Air Bersih</label>
		  <div class="controls">
			<input type="text" class="input-xlarge" id="sumber_air_bersih" name="sumber_air_bersih" rel="popover" data-content="Masukkan jumlah sumber air bersih" data-original-title="Sumber Air Bersih">
		   
		  </div>
		</div>
		
		<div class="control-group">
		<label class="control-label" for="input01">Longitude</label>
		  <div class="controls">
			<input type="text" class="input-xlarge" id="longitude" name="longitude" rel="popover" data-content="Masukkan koordinat" data-original-title="Longitude">
		   
		  </div>
		</div>
		
		<div class="control-group">
		<label class="control-label" for="input01">Latitude</label>
		  <div class="controls">
			<input type="text" class="input-xlarge" id="latitude" name="latitude" rel="popover" data-content="Masukkan koordinat" data-original-title="Latitude">
		   
		  </div>
		</div>
		
			<div class="control-group">
		<label class="control-label" for="input01">Polygon</label>
		  <div class="controls">
			<textarea class="input-xlarge" id="dataPanel" name="polygon"></textarea>
			<button id="showData" type="button" class="btn btn-primary">Tampilkan Koordinat</button>
			<button id="reset" value="Reset" type="button" class="btn btn-warning">Reset Polygon</button>
		  </div>
	</div>
		<div class="control-group">
			<label class="control-label" for="input01"></label>
			  <div class="controls">
			   <button type="submit" class="btn btn-success" >Tambah Kabupaten</button>
			  </div>
		</div>
		  </fieldset>
	</form>
		</div>
</div>

<?php

if ($_GET['action'] == "add") {
	
	/**
		$nama_kab	= htmlentities(mysql_real_escape_string($_POST['kabupaten']));
		$wilayah		= htmlentities(mysql_real_escape_string($_POST['luas_wilayah']));
		$lat				= htmlentities(mysql_real_escape_string($_POST['latitude']));
		$long			= htmlentities(mysql_real_escape_string($_POST['longitude']));
	**/
	
	$db = new Kabupaten();
	$kab = array ('nama_kabkota'=>$_POST['kabupaten'],
				  'luas_wilayah'=>$_POST['luas_wilayah'],
				  'kepadatan_penduduk'=>$_POST['kepadatan_penduduk'],
				  'tata_guna_lahan'=>$_POST['tata_guna_lahan'],
				  'topografi_wilayah'=>$_POST['topografi_wilayah'],
				  'sumber_air_bersih'=>$_POST['tata_guna_lahan'],
				  'lat'=>$_POST['latitude'],
				  'long'=>$_POST['longitude'],
				  'polygon'=>$_POST['polygon']);
				  
	$kabkota = $db->tambah("kabkota", $kab);

	if ($kabkota) {
		?>
			<script language="javascript">
			document.location="?page=addkab&success=1";
			</script>
		<?php
		
	} else {
		?>
				<script language="javascript">
				document.location="?page=addkab&success=0";
				</script>
		<?php
		
	}
	
}





